<?php

/**
 * Profile information controller
 */
class Profileinformationcontroller {

	/**
	 * Constructor
	 * @param Registry $registry
	 * @param int $user the user id
	 * @return void
	 */
	public function __construct( $registry, $directCall=true, $user, $profileID )
	{
		$this->registry = $registry;
		$urlBits = $this->registry->getObject('url')->getURLBits();
		if( isset( $urlBits[1] ) )
		{
			switch( $urlBits[1] )
			{
				case 'edit':
					$this->editProfile($profileID);
					break;
				//case 'view':
				//	$this->createProfile();
				//	break;
				case 'create':
					$this->createProfile();
					break;
				default:
					$this->viewProfile( $user, $profileID);
					break;
			}
		}
		else
		{
			$this->viewProfile( $user, $profileID);
		}

	}

	/**
	 * View a users profile information
	 * @param int $user the user id
	 * @return void
	 */
	private function viewProfile( $user, $profile_id)
	{
		// load the template
		$this->registry->getObject('template')->buildFromTemplates('main/main.tpl.php', 'main/main_menu.tpl.php', 'main/main_content.tpl.php', 'profile/information/view.tpl.php' ,'main/footer.tpl.php');
		// get all the profile information, and send it to the template
		require_once( FRAMEWORK_PATH . 'models/profile.php' );
		$profile = new Profile( $this->registry, $user, $profile_id);
		$profile->toTags( 'p_' );
	}

	private function editProfile($profileID)
	{
		$this->registry->getObject('template')->buildFromTemplates( 'profile/information/information.tpl.php');
		
		if( $this->registry->getObject('authenticate')->isLoggedIn() == true )
		{
			$user = $this->registry->getObject('authenticate')->getUser()->getUserID();
			require_once( FRAMEWORK_PATH . 'models/profile.php' );
			$profile = new Profile( $this->registry, $user, $profileID);
				
			if( isset( $_POST ) && count( $_POST ) > 0 && isset($_POST['section']))
			{
				$section = $_POST['section'];
				switch($section)
				{
					case 'basic':
					{
						$profile->setAbout($this->registry->getObject('db')->sanitizeData( $_POST['about'] ) );
						$profile->setName( $this->registry->getObject('db')->sanitizeData( $_POST['name'] ) );
						$profile->setEmail($this->registry->getObject('db')->sanitizeData( $_POST['email'] ) );
						$profile->setDOB($this->registry->getObject('db')->sanitizeData( $_POST['dob'], false ) );
						$profile->setGender($this->registry->getObject('db')->sanitizeData( $_POST['gender'] ), true );
						$profile->setAddress($this->registry->getObject('db')->sanitizeData( $_POST['address'] ), true );
						$profile->setProfilename($this->registry->getObject('db')->sanitizeData( $_POST['profilename'] ) );
						$profile->save();
					}
					break;
					case 'education':
					{
						$profile->setEmployer($this->registry->getObject('db')->sanitizeData( $_POST['employer'] ) );
						$profile->setCollege($this->registry->getObject('db')->sanitizeData( $_POST['college'] ) );
						$profile->setHighSchool($this->registry->getObject('db')->sanitizeData( $_POST['highschool'] ) );
						$profile->save();
					}
					break;
					case 'photo':
					{								
						$this->uploadPhoto($profile);	
					}
					break;
					case 'passwordchange':
						{
							$password = md5($_POST['newpassword']);
							$confirmPwd = md5($_POST['confirmpassword']);
							
							if ($password != $confirmPwd)
							{
								echo '<result>2</result>';
							}
							else if (strlen($_POST['newpassword']) < 6)
							{
								echo '<result>1</result>';
							} 
							else
							{
								$changes = array();
								$changes['password_hash'] = $password;
								$this->registry->getObject('db')->updateRecords( 'users', $changes, 'uid=' . $user);
								echo '<result>0</result>';
							}
						}
						break;
					default:
					break;
				}				
			}
			else
			{
				$this->registry->getObject('template')->buildFromTemplates( 'main/main.tpl.php', 'main/main_menu.tpl.php', 'main/main_content.tpl.php' ,'profile/information/edit.tpl.php', 'main/footer.tpl.php' );
				if ($profile->getIsDefault())
				{
					$this->registry->getObject('template')->addTemplateBit('CHANGE_PWD', 'profile/information/changepwd.tpl.php');
				}
				else
				{
					$this->registry->getObject('template')->addTemplateBit('CHANGE_PWD', '');
				}
				
				$profile->toTags( 'p_' );
			}
		}
	}
	
	private function createProfile()
	{
		if( isset( $_POST ) && count( $_POST ) > 0)
		{
				if (isset($_POST['action']))
				{
					switch($_POST['action'])
					{
						case 'saveOtherProfile':
							{
								$user = $this->registry->getObject('authenticate')->getUser()->getUserID();
								$insert = array();
								$insert['user_id'] = $user;
								
								if (isset($_POST['about']))
									$insert['about'] = $this->registry->getObject('db')->sanitizeData( $_POST['about'] );
								if (isset($_POST['name']))
									$insert['name'] = $this->registry->getObject('db')->sanitizeData( $_POST['name'] );
								if (isset($_POST['email']))
									$insert['email'] = $this->registry->getObject('db')->sanitizeData( $_POST['email'] );
								if (isset($_POST['dob']))
									$insert['dob'] = $this->registry->getObject('db')->sanitizeData( $_POST['dob']);
								if (isset($_POST['gender']))
									$insert['gender'] = $this->registry->getObject('db')->sanitizeData( $_POST['gender']);
								if (isset($_POST['address']))
									$insert['address'] = $this->registry->getObject('db')->sanitizeData( $_POST['address'] );
								if (isset($_POST['profile_name']))
									$insert['profile_name'] = $this->registry->getObject('db')->sanitizeData( $_POST['profilename'] );
								if (isset($_POST['employer']))
									$insert['employer'] = $this->registry->getObject('db')->sanitizeData( $_POST['employer'] );
								if (isset($_POST['college']))											
									$insert['college'] = $this->registry->getObject('db')->sanitizeData( $_POST['college'] );
								if (isset($_POST['profilename']))
									$insert['profile_name'] = $this->registry->getObject('db')->sanitizeData( $_POST['profilename'] );
								
								$this->registry->getObject('db')->insertRecords( 'profile', $insert );	

								$profileID = $this->registry->getObject('db')->lastInsertID();
								
								require_once( FRAMEWORK_PATH . 'models/profile.php' );
								$profile = new Profile( $this->registry, $user, $profileID);	

								$url = $this->registry->getObject('url')->buildURL( array('profile', 'home', $user, $profileID), '', false );
								$this->registry->redirectUser( $url);
							}
							break;
						case 'saveFBProfile':
							{
								
							}
							break;
						case 'facebook':
							{
								$profile = $this->registry->getObject('authenticate')->getUser()->getCurrentProfile();
								$CurrentProfileID = $profile->getProfileID();
								$userID = $this->registry->getObject('authenticate')->getUser()->getUserID();
								
								$this->registry->getObject('template')->buildFromTemplates( 'profile/information/createfb.tpl.php');
								$this->registry->getObject('template')->getPage()->addTag( 'user_id', $userID );
								$this->registry->getObject('template')->getPage()->addTag( 'profile_id', $CurrentProfileID );
							}
							break;
						default:
							{
								$genders = array();
								$genders[] = 'Male';
								$genders[] = 'Female';

								$gendervalues = array();

								foreach( $genders as $gender )
								{
									$values = array();
									$values['gender'] = $gender;									
								
									$values['optionselected'] = "";
										
									$gendervalues[] = $values;
								}						
								
								$this->registry->getObject('template')->buildFromTemplates( 'profile/information/createprofile.tpl.php');
								
								$cache = $this->registry->getObject('db')->cacheData($gendervalues);
								$this->registry->getObject('template')->getPage()->addTag( 'genderoptions', array( 'DATA', $cache) );
								
							}
							break;
					}
				}
				else
				{				
					
					$this->registry->getObject('template')->buildFromTemplates( 'profile/information/create.tpl.php');				
							
				}
		}	
		else
		{
			$this->registry->getObject('template')->buildFromTemplates( 'main/main.tpl.php', 'main/main_menu.tpl.php', 'main/main_content.tpl.php' ,'profile/information/create.tpl.php', 'main/footer.tpl.php' );
		}
	}

	private function uploadPhoto($profile)
	{;
		$fileElementName = 'profile_photo';
		$error = "";
		$msg = "";

		if(!empty($_FILES[$fileElementName]['error']))
		{
			switch($_FILES[$fileElementName]['error'])
			{

				case '1':
					$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
					break;
				case '2':
					$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
					break;
				case '3':
					$error = 'The uploaded file was only partially uploaded';
					break;
				case '4':
					$error = 'No file was uploaded.';
					break;

				case '6':
					$error = 'Missing a temporary folder';
					break;
				case '7':
					$error = 'Failed to write file to disk';
					break;
				case '8':
					$error = 'File upload stopped by extension';
					break;
				case '999':
				default:
					$error = 'No error code avaiable';
			}
		}
		elseif(empty($_FILES[$fileElementName]['tmp_name']) || $_FILES[$fileElementName]['tmp_name'] == 'none')
		{
			$error = 'No file was uploaded..';
		}
		else
		{
			$msg .= " File Name: " . $_FILES[$fileElementName]['name'] . ", ";
			$msg .= " File Size: " . @filesize($_FILES[$fileElementName]['tmp_name']);
			
			require_once( FRAMEWORK_PATH . 'lib/images/imagemanager.class.php' );
			$im = new Imagemanager();
			$im->loadFromPost( 'profile_photo', $this->registry->getSetting('upload_path') .'profile/', time() );
			if( $im == true )
			{
				$im->resizeScaleHeight( 150 );
				$im->save( $this->registry->getSetting('upload_path') .'profile/' . $im->getName() );
				$profile->setPhoto( 'uploads/profile/'.$im->getName() );
			}
		}

		$profiles = $this->registry->getObject('authenticate')->getUser()->getProfiles();
		
		foreach ( $profiles as $userprofile)
		{			
			if ($userprofile['profile_id'] == $profile->getProfileID())
			{
				$defaultProfInfo['photo'] = $profile->getPhoto();
			}
		}
		
		$profile->save();
		
		$this->registry->getObject('template')->buildFromTemplates('main/main.tpl.php', 'main/main_menu.tpl.php', 'main/main_content.tpl.php', 'profile/information/edit.tpl.php');
		$this->registry->getObject('authenticate')->getUser()->loadProfiles();
				
		$cache = $this->registry->getObject('db')->cacheData(  $this->registry->getObject('authenticate')->getUser()->getProfiles() );
		$this->registry->getObject('template')->getPage()->addTag( 'profiles', array( 'DATA', $cache) );
		$this->registry->getObject('template')->getPage()->addTag('sitename', $this->registry->getSetting('sitename'));
					
		$profile->toTags( 'p_' );
		
		$currentProfile = $profile;
		
		$this->registry->getObject('template')->getPage()->addTag( 'current_profile_name', $currentProfile->getName());
		$this->registry->getObject('template')->getPage()->addTag( 'current_profile', $currentProfile->getProfileName());
		$this->registry->getObject('template')->getPage()->addTag( 'current_profile_photo', $currentProfile->getPhoto());
		$this->registry->getObject('template')->getPage()->addTag( 'current_profile_id', $currentProfile->getProfileID());		
		
		$this->registry->getObject('template')->getPage()->addTag( 'current_view_profile_name', $currentProfile->getName());
		$this->registry->getObject('template')->getPage()->addTag( 'current_view_profile', $currentProfile->getProfileName());
		$this->registry->getObject('template')->getPage()->addTag( 'current_view_profile_photo', $currentProfile->getPhoto());
		$this->registry->getObject('template')->getPage()->addTag( 'current_view_profile_id', $currentProfile->getProfileID());

		if ($profile->getIsDefault())
		{
			$this->registry->getObject('template')->addTemplateBit('CHANGE_PWD', 'profile/information/changepwd.tpl.php');
		}
		else
		{
			$this->registry->getObject('template')->addTemplateBit('CHANGE_PWD', '');
		}
		
		$this->registry->getObject('template')->parseOutput();
				
	}
}


?>
